<?php
/*
Plugin Name: BUETian.com terminal [BOT]
Plugin URI: http://www.buetian.com
Description: One stop service to post as WP post and FB new page entry
Author: seoul
Version: 1.0
*/
function session_start_function(){
 if ( session_id() == '') { // no session has been started yet, which is needed for validation
      session_start();
    }
}
add_action('init', 'session_start_function');

add_action('admin_menu', 'WPTL_terminal');

function WPTL_terminal() {

	add_menu_page("WPTL- Admin- Options", "Terminal window", 'manage_options', 'WPTL-top-level-handle', 'WPTL_sublevel_home' );
	// Add a submenu to the custom top-level menu:

	// Add a submenu to the custom top-level menu:
    add_submenu_page('WPTL-top-level-handle', "Settings", "Settings", 'manage_categories', 'WPTL-top-level-handle', 'WPTL_sublevel_settings');
	
	// Add a submenu to the custom top-level menu:
	add_submenu_page('WPTL-top-level-handle', "Insert new entry", "New FB post", 'manage_categories', 'WPTL-sub-insert-facebook', 'WPTL_sublvl_insert_facebook');
	
	// Add a submenu to the custom top-level menu:
	add_submenu_page('WPTL-top-level-handle', "Push table to wp", "Push table to wp", 'manage_categories', 'WPTL-sub-push-table2wp', 'WPTL_sublvl_push_table2wp');
	
	// Add a submenu to the custom top-level menu:
	add_submenu_page('WPTL-top-level-handle', "Push WP post to FB", "WP post to FB page", 'manage_categories', 'WP2FB-sub-new-post', 'WP2FB_sublvl_new_post');

	// Add a submenu to the custom top-level menu:
	add_submenu_page('WPTL-top-level-handle', "Import RSS", "Import RSS", 'manage_categories', 'RSS-sub-new-import', 'RSS_sublvl_new_import');
	
	// Add a submenu to the custom top-level menu:
	add_submenu_page('WPTL-top-level-handle', "Import Facebook-Event", "Import FB Event", 'manage_categories', 'WPTL-sub-new-import-event', 'WPTL_sublvl_new_import_event');
	
	// Add a submenu to the custom top-level menu:
	add_submenu_page('WPTL-top-level-handle', "Import Facebook-Album", "Import FB Album", 'manage_categories', 'WPTL-sub-new-import-album', 'WPTL_sublvl_new_import_album');
	
	// Add a submenu to the custom top-level menu:
	add_submenu_page('WPTL-top-level-handle', "Sandbox terminal", "Sandbox", 'manage_categories', 'Sandbox-sub-new-post', 'Sandbox_sublvl_new_post');
}

// Add settings link on plugin page
function wptl_link($links) {
  $settings_link = '<a href="admin.php?page=WPTL-top-level-handle">Settings</a>';
  array_unshift($links, $settings_link);
  return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'wptl_link' );


add_action('wp_ajax_WPTL_sublvl_new_import_album', 'WPTL_sublvl_new_import_album_action_callback');
function WPTL_sublvl_new_import_album_action_callback() {
	if( current_user_can('manage_categories') )
		WPTL_sublvl_new_import_album();
}
function WPTL_sublvl_new_import_album()
{
	include_once ('window.terminal.importAlbum.php');
	try{
		$WPTL = new ImportFacebookAlbum('WPTL-sub-new-import-album', 'WPTL_sublvl_new_import_album');
		$WPTL->processAlbum();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

add_action('wp_ajax_WPTL_sublvl_new_import_event', 'WPTL_sublvl_new_import_event_action_callback');
function WPTL_sublvl_new_import_event_action_callback() {
	if( current_user_can('manage_categories') )
		WPTL_sublvl_new_import_event();
}
function WPTL_sublvl_new_import_event()
{
	include_once ('window.terminal.importEvent.php');
	try{
		$WPTL = new ImportFacebookEvent('WPTL-sub-new-import-event', 'WPTL_sublvl_new_import_event');
		$WPTL->processEvent();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function WP2FB_sublvl_new_post()
{
	include_once ('window.terminal.publish2fb.php');
	try{
		$WPTL = new SimpleTextbox('WP2FB-sub-new-post');
		$WPTL->publishFromWordpressPostID();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function RSS_sublvl_new_import()
{
	include_once('window.terminal.importRSS.php');
	try{
		$obj = new ImportRSS('RSS-sub-new-import', 'RSS_sublvl_new_import');
		$obj->processRSS();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

add_action('wp_ajax_RSS_sublvl_new_import', 'RSS_sublvl_new_import_action_callback');
function RSS_sublvl_new_import_action_callback() {
	if( current_user_can('manage_categories') )
		RSS_sublvl_new_import();
}

function Sandbox_sublvl_new_post()
{
	include_once('sandbox.php');
	try{
		$internet = new Sandbox('Sandbox-sub-new-post');
		$internet->flickrOAUTH();	
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function WPTL_sublvl_push_table2wp()
{
	include_once ('window.terminal.table2wp.php');
	try{
		$WPTL = new Table2wp('WPTL-sub-push-table2wp');
		$WPTL->pushTable2wp();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function WPTL_sublvl_insert_facebook()
{
	include_once ('window.terminal.publish2fb.php');
	try{
		$WPTL = new SimpleTextbox('WPTL-sub-insert-facebook');
		$WPTL->terminalPost();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function WPTL_sublevel_settings()
{
	include_once('wpfb.group.home.php');
	try{
		$object= new Settings('WPTL-top-level-handle');		
		$object->renderHTMLforTerminal();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function WPTL_sublevel_home()
{
	;
}
?>